package com.cyx.dao;

import com.cyx.entity.Seckill;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

public interface SeckillMapper extends JpaRepository<Seckill,Long> {
    /**
     * 根据传过来的<code>seckillId</code>去减少商品的库存.
     *
     * @param seckillId 秒杀商品ID
     * @param killTime  秒杀的精确时间
     * @return 如果秒杀成功就返回1，否则就返回0
     *  AND DATEDIFF(NOW(),start_time)>0 and DATEDIFF(NOW(),end_time)<0 AND number > 0
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE Seckill SET number = number - 1 WHERE seckill_id = ?1")
    int reduceNumber(long seckillId);

    /**
     * 根据一个偏移量去查询秒杀的商品列表.
     *
     * @param offset 偏移量
     * @param limit  限制查询的数据个数
     * @return 符合偏移量查出来的数据个数
     */
    @Query(value = "SELECT * FROM Seckill AS s ORDER BY create_time DESC LIMIT ?1, ?2",nativeQuery = true)
    List<Seckill> queryAll(int offset,int limit);
}
